#version 150

#moj_import <fog.glsl>
#moj_import <global.glsl>

uniform sampler2D Sampler0;

uniform vec4 ColorModulator;
uniform float FogStart;
uniform float FogEnd;
uniform vec4 FogColor;

in float vertexDistance;
in vec4 vertexColor;
in vec4 vertexColor0;//vertexColor without shadow and fog
in vec4 light;
in vec2 texCoord0;
in vec2 texCoord1;
in vec4 normal;

out vec4 fragColor;

vec3 colors[] = vec3[](
	vec3(0,255,0)/255,//green screen
	vec3(55,92,196)/255,//water
	vec3(31,31,159)/255,//night
	vec3(77,145,48)/255,//posion
	vec3(225,152,57)/255,//fire resistance
	vec3(116,90,97)/255,//the turtle master
	vec3(71,76,71)/255,//weakness
	vec3(126,129,144)/255,//invisibility
	vec3(45,81,151)/255,//water breathing
	vec3(66,10,9)/255,//harming
	vec3(203,91,169)/255,//regeneration
	vec3(145,36,35)/255,//strength
	vec3(34,252,75)/255,//leaping
	vec3(245,36,35)/255,//healing
	vec3(50,151,0)/255,//luck
	vec3(252,236,207)/255,//slow falling
	vec3(89,107,127)/255,//slowness
	vec3(123,173,196)/255//swiftness
);

void main() {
	//vanilla code
	vec4 color = texture(Sampler0, texCoord0) * vertexColor * ColorModulator;
    if (color.a < 0.1)discard;
    fragColor = linear_fog(color, vertexDistance, FogStart, FogEnd, FogColor);
	
	float alpha = textureLod(Sampler0, texCoord0, 0.0).a * 255.0;//get alpha from lod
	if(check_alpha(alpha,OVERLAY_ALPHA)||check_alpha(alpha,POTION_ALPHA)){//potion overlayey
	//Coord
		vec2 Coord;float n;float m;
		//custom potion color
			n = OVERLAY_SIZE / UNIT_SIZE - 1.0;//x-translation
			m = OVERLAY_SIZE / UNIT_SIZE - 1.0;//y-translation
		//vanilla potion color
		for(int i = 0; i < colors.length(); i++){
			if(check_rgb(vertexColor0.rgb, colors[i])){
				n = i % int(OVERLAY_SIZE / UNIT_SIZE);
				m = (i - n) / (OVERLAY_SIZE / UNIT_SIZE);
			}
		}
	//color
		Coord = vec2(texCoord0.x, texCoord0.y) / (OVERLAY_SIZE / UNIT_SIZE) + vec2(n, m) / (UNIT_SIZE * 4.0);
		color = texture(Sampler0, Coord) * ColorModulator;//bottle color
		if(check_alpha(textureLod(Sampler0, Coord, 0.0).a * 255.0, POTION_ALPHA))
			color = texture(Sampler0, Coord) * vertexColor0 * ColorModulator;//potion color
		//fog and light
		fragColor = linear_fog(color, vertexDistance, FogStart, FogEnd, FogColor);	
		if(check_rgb(fragColor.rgb,colors[0])) discard;//remove green screen
		fragColor *= light;
	}
}